/*************************************************************************
*
*	sendretr.c
*
*  Copyright 2007,2008 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions which send or retrieve files from
*    the dataserver.
*
**************************************************************************
*
*  version 1.2, 11/13/08, SBS
*    - modify put/fetchfile to use rsync & wget and new config vars
*
*  version 1.1.5, 10/22/08, SBS
*    - modify putfile/fetchfile to take a complete filespec, and for 
*        non-explicit identity file
*        (identity file is now handled through ~/.ssh/config)
*    - cleanup / doc'n
*
*  version 1.1.4, 10/4/08, SBS
*    - documentation
*
*  version 1.1.3, 3/21/08, SBS
*    - minor cleanup
*
*  version 1.1.2, 3/9/08, SBS
*    - add FetchFileS
*
*  version 1.1.1, 1/28/08, SBS
*    - "static char" -> "char"
*    - minor cleanup
*
*  version 1.1, 10/3/07, SBS
*    - moved chmod_key into here
*
*  version 1.0, 7/3/07, SBS
*    - split from archiver.c
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup common
*  @brief
*  This module provides functions which send or retrieve files from
*    the dataserver.
*  <hr>
**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "logfile.h"

#include "sendretr.h"

#define CMDSTRLENGTH 512
#define FILENAMELENGTH 128


/************************************************
*
* sendretr_FetchFile()
*/
/** 
*
* @brief
* Retrieves a file from the dataserver.
*
* Parameters:
*   - cf       - configuration data
*   - robnum   - robot number
*   - filebase - filename base ("command" or "telemetry")
*
* Returns: 
*   - none
*
************************************************/
int sendretr_FetchFileNew(
			CONFIGDATA cf
			,char *filespec
			)
{
	char cmd[CMDSTRLENGTH];

	int rval;

	#ifdef DEBUG
	char temp[LOGSTRLEN];
	#endif


//	snprintf(cmd
//			,CMDSTRLENGTH
//			,"%s %s:%s/%s %s"
//			,cf.retrcommand
//			,cf.servername
//			,cf.remotepath
//			,filespec
//			,cf.localpath);

//	snprintf(cmd,CMDSTRLENGTH
//				,"rsync -vz %s::%s/%s %s"
//				,cf.rsynchostname
//				,cf.rsyncsharename
//				,filespec
//				,cf.rsynclocalpath);

	snprintf(cmd,CMDSTRLENGTH
				,"wget -O %s/%s http://%s/%s/%s"
				,cf.localdatapath
				,filespec
				,cf.wgethostname
				,cf.wgethostpath
				,filespec);

	#ifdef DEBUG
	snprintf(temp,LOGSTRLEN,"sendretr_FetchFileNew() --- Command is %s\n",cmd);
	logfile_LogPrint(temp);
	#endif

	rval = system(cmd);
	
	#ifdef DEBUG
	snprintf(temp, LOGSTRLEN, "sendretr_FetchFileNew() --- rsync returned %d\n",rval);
	logfile_LogPrint(temp);
	#endif			
	
	return(rval);
}
	

/************************************************
*
* sendretr_PutFile()
*/
/** 
*
* @brief
* Sends a file to the dataserver
*
* Parameters:
*   - cf       - configuration data
*   - filespec - filename to upload
*
* Returns:
*   - none
*
************************************************/
void sendretr_PutFileNew(
			CONFIGDATA cf
			,char *filespec
			)
{
	char cmd[CMDSTRLENGTH];

	#ifdef DEBUG
	char temp[LOGSTRLEN];
	#endif

//	snprintf(cmd
//			,CMDSTRLENGTH
//			,"%s %s/%s %s@%s:%s"
//			,cf.sendcommand
//			,cf.localpath
//			,filespec
//			,cf.username
//			,cf.servername
//			,cf.remotepath);
			
	snprintf(cmd,CMDSTRLENGTH
				,"rsync -vz %s/%s %s::%s"
				,cf.localdatapath
				,filespec
				,cf.rsynchostname
				,cf.rsyncsharename);		
				
			
	#ifdef DEBUG
	snprintf(temp,LOGSTRLEN,"=====SBS===== Command is %s\n",cmd);
	logfile_LogPrint(temp);
	#endif

	system(cmd);

	return;
}
